package com.sims.service;

import java.util.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sims.bean.Class;
import com.sims.bean.Student;
import com.sims.bean.StudentPosition;
import com.sims.dao.IStudentDAO;
import com.sims.dao.IStudentPositionDAO;
import com.sims.util.Constant;
import com.sims.util.HttpSessionUtil;

public class StudentService {
	@Autowired
	private IStudentDAO studentDAO;
	@Autowired
	private IStudentPositionDAO studentPositionDAO;
	
	/** 学生登录。传入sno和password。
	 * @param student
	 * @return
	 */
	public Map<String, String> studentLogin(Student student) {
		Map<String, String> result = new HashMap<String, String>();
		
		Student stuInDB = studentDAO.findBySno(student.getSno());
		
		if (stuInDB==null) {
			result.put(Constant.ERROR, "Wrong Account Or Password");
			return result;
		}
		else if (!stuInDB.getPassword().trim().equals(student.getPassword()) ) {
			result.put(Constant.ERROR, "Wrong Account Or Password");
			return result;
		}
		
		BeanUtils.copyProperties(stuInDB, student);
		
		HttpSessionUtil.login(stuInDB.getSid(), Constant.STUDENT, stuInDB.getCid());
		result.put(Constant.SUCCESS, null);
		
		return result;
	}
	
	public Map<String, String> studentLogoff() {
		Map<String, String> result = new HashMap<String, String>();
		HttpSessionUtil.logout();
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	/**	只有学生可调用。修改学生密码。只需传student.password。
	 * @param student
	 * @return
	 */
	public Map<String, String> modifyStudentPassword(Student student) {
		Map<String, String> result = new HashMap<String, String>();
		
		Student s = studentDAO.findById(HttpSessionUtil.getUserId());
		if (s==null) {
			result.put(Constant.ERROR, "Student Not Exists");
			return result;
		}
		s.setPassword(student.getPassword());
		studentDAO.attachDirty(s);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	/** 学生、老师都可调用。找出学生基本信息。传入student.sid。
	 * @param student
	 * @param studentPosition
	 * @return
	 */
	public Map<String, String> findStudentBasicInfo(Student student, StudentPosition studentPosition) {
		//basic info + position
		Map<String, String> result = new HashMap<String, String>();
		
		Student s = studentDAO.findById(student.getSid());
		if (s==null) {
			result.put(Constant.ERROR, "Student Not Exists");
			return result;
		}
		
		StudentPosition sp = new StudentPosition();
		
		if ( studentPositionDAO.findBySno(s.getSno()).isEmpty()) {
			sp.setPosition("无");
		}
		else sp = (StudentPosition)studentPositionDAO.findBySno(s.getSno()).get(0);
		
		BeanUtils.copyProperties(s, student);
		BeanUtils.copyProperties(sp, studentPosition);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	/** 只学生可调用。修改学生基本信息。传入学生修改后的基本信息（不包括学生职务，不需sid）。
	 * @param student
	 * @return
	 */
	public Map<String, String> modifyStudentBasicInfo(Student student) {
		Map<String, String> result = new HashMap<String, String>();

		Student s = studentDAO.findById(HttpSessionUtil.getUserId());
		
		if (s==null) {
			result.put(Constant.ERROR, "Student Not Exists");
			return result;
		}
		//可修改项：
		if (student.getBirthday() != null) s.setBirthday(student.getBirthday());
		if (student.getIdcard().length() != 0) s.setIdcard(student.getIdcard());
		if (student.getProfile().length() != 0) s.setProfile(student.getProfile());
		if (student.getTel().length() != 0) s.setTel(student.getTel());
		
		studentDAO.attachDirty(s);
		//再加一个copy
		BeanUtils.copyProperties(s, student);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	/** 学生、老师都可调用。找出班级学生所有信息。传入Tclass.cid。参数返回List。
	 * @param Tclass
	 * @param studentTelList
	 * @return
	 */
	public Map<String, String> findClassStudent(List<Student> studentList) {
		Map<String, String> result = new HashMap<String, String>();
		
		List<Student> dbList = studentDAO.findByCid(HttpSessionUtil.getUserClassId());
		
		for(Student student : dbList) {
			studentList.add(student);
		}
		
		if (studentList == null) {
			result.put(Constant.ERROR, "No Records Exist");
			return result;
		}
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	/** 只学生调用。修改学生自己通讯录。传入student.tel。
	 * @param student
	 * @return
	 */
	public Map<String, String> modifyTel(Student student) {
		Map<String, String> result = new HashMap<String, String>();
		
		Student s = studentDAO.findById(HttpSessionUtil.getUserId());
		s.setTel(student.getTel());
		
		studentDAO.attachDirty(s);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	
}
